package com.numberone.master.modules.recharge.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.recharge.entity.Package;
import com.numberone.master.modules.recharge.service.PackageService;
import com.numberone.master.modules.sys.entity.Dict;
import com.numberone.master.modules.sys.entity.DictInfo;
import com.numberone.master.modules.sys.entity.Office;
import com.numberone.master.modules.sys.service.DictService;
import com.numberone.master.modules.sys.service.OfficeService;
import com.numberone.master.modules.sys.service.SystemService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * 物联卡套餐Controller
 * @author chengkang
 * @version 2018-02-11
 */
@Controller
@RequestMapping(value = "${adminPath}/recharge/package")
public class PackageController extends BaseController {

	@Autowired
	private PackageService packageService;
	
	@Autowired
	private OfficeService officeService;
	
	@Autowired
	private DictService dictService;
	

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public Package get(@RequestParam(required=false) String id) {
		Package entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = packageService.get(id);
		}
		if (entity == null){
			entity = new Package();
		}
		return entity;
	}

	 /**
     * list列表
     * @param package
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("recharge:package:list")
	@RequestMapping(value = {"list", ""})
	public String list(Package p, HttpServletRequest request, HttpServletResponse response, Model model) {
		//查询所属的账号
		Office office = UserUtils.getUser().getOffice();
		p.setOffice(office);
		Page<Package> page = packageService.findPage(new Page<Package>(request, response), p); 
		model.addAttribute("page", page);
		model.addAttribute("userId", office.getId());
		return "modules/recharge/packageList";
	}
	
	/**
	 * 根据所属id查询
	 * @param office_id
	 * @return
	 */
	@RequestMapping(value = {"getPackList", ""})
	@ResponseBody
	public List<Package> getPackList(String office_id) {
		//查询所属的账号
		Office office = UserUtils.get(office_id).getOffice();
		Package p = new Package();
		p.setOffice(office);
		List<Package> list = packageService.findList(p);
		return list;
	}
	
	/**
	 * 已经分配套餐列表
	 * @param p
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:already:list")
	@RequestMapping(value = {"already_list", ""})
	public String already_list(Package p, HttpServletRequest request, HttpServletResponse response, Model model) {
		//查询自己的代理商id
		Office office = UserUtils.getUser().getOffice();
//		Office o = new Office();
//		o.setParentsId(office.getId());
//		List<Office> officeList = officeService.findAlreadyList(o);
//		//查询所属的账号
//		String ids = "";
//		if(officeList.size() > 0){
//			for (int i = 0; i < officeList.size(); i++) {
//				ids+= officeList.get(i).getId()+",";
//			}
//		}
//		String idList =ids+office.getId();
//		p.setIds(idList);
		p.setCreateId(office.getId());
		//p.setOffice(office);
		Page<Package> page = packageService.findPage(new Page<Package>(request, response), p); 
		model.addAttribute("page", page);
		return "modules/recharge/alreadyList";
	}
	
	/**
	 * 已有套餐删除
	 * @param p
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:already:delete")
	@RequestMapping(value = "alreadyDelete")
	public String alreadyDelete(Package p, RedirectAttributes redirectAttributes) {
		packageService.delete(p);
		addMessage(redirectAttributes, "删除物联卡套餐成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/package/already_list?repage";
	}
	
	/**
	 * 打开已有套餐修改
	 * @param p
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:already:update")
	@RequestMapping(value = "openAlreadyUpdate")
	public String openAlreadyUpdate(Package p, Model model) {
		Office office = UserUtils.getUser().getOffice();
		model.addAttribute("userId", office.getId());
		model.addAttribute("p", p);
		return "modules/recharge/alreadyUpdate";
	}
	
	/**
	 * 修改
	 * @param p
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:package:update")
	@RequestMapping(value = "alreadyUpdate")
	public String alreadyUpdate(Package p, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, p)){
			return openUpdate(p, model);
		}
		packageService.update(p);
		addMessage(redirectAttributes, "修改物联卡套餐成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/package/already_list?repage";
	}
	
	

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("recharge:package:save")
	@RequestMapping(value = "openSave")
	public String openSave(Model model) {
		//查询套餐没有的id
		Office o = UserUtils.getUser().getOffice();
		Package p = new Package();
		p.setOffice(o);
		List<Package> packageList = packageService.findList(p);
		String value = "";
		if(packageList.size() > 0){
			for (int i = 0; i < packageList.size(); i++) {
				value += packageList.get(i).getTcId()+",";
			}
			value = value.substring(0,value.length() - 1);//去除最后一个逗号标识
			List<DictInfo> dictList = dictService.getNotDictList("pkg",value);
			model.addAttribute("dictList", dictList);
		}
		
		return "modules/recharge/packageSave";
	}


	/**
	 * 打开修改页面
	 * @param package
	 * @param model
	 * @return
	 */
	@RequiresPermissions("recharge:package:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(Package p, Model model) {
		Office office = UserUtils.getUser().getOffice();
		model.addAttribute("userId", office.getId());
		model.addAttribute("p", p);
		return "modules/recharge/packageUpdate";
	}

	/**
	 * 添加
	 * @param package
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:package:save")
	@RequestMapping(value = "save")
	public String save(Package p, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, p)){
			return openSave(model);
		}
		//查询所属的账号
		Office office = UserUtils.getUser().getOffice();
		p.setOffice(office);
		packageService.save(p);
		addMessage(redirectAttributes, "添加物联卡套餐成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/package/list?repage";
	}

	/**
	 * 修改
	 * @param package
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("recharge:package:update")
	@RequestMapping(value = "update")
	public String update(Package p, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, p)){
			return openUpdate(p, model);
		}
		packageService.update(p);
		addMessage(redirectAttributes, "修改物联卡套餐成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/package/list?repage";
	}

	 /**
     * 删除
     * @param package
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("recharge:package:delete")
	@RequestMapping(value = "delete")
	public String delete(Package p, RedirectAttributes redirectAttributes) {
		packageService.delete(p);
		addMessage(redirectAttributes, "删除物联卡套餐成功");
		return "redirect:"+Global.getAdminPath()+"/recharge/package/list?repage";
	}
	
	/**
	 * 销售价格和单价比较
	 * @param price
	 * @param salePrice
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "isPriceCompare")
	public boolean isPriceCompare(String hPrice, String salePrice) {
		if(StringUtils.isEmpty(hPrice) || StringUtils.isEmpty(salePrice)){
			return true;
		}else{
			if(salePrice.compareTo(hPrice) > 0){
				return true;
			}else{
				return false;
			}
		}
	}

}